﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace FJASPX.Framwork.Query
{
    /// <summary>
    /// Page 分页对象. 包含当前页数据及分页信息如总记录数.
    /// </summary>
    [Serializable]
    public class Page
    {
        private static int DEFAULT_PAGE_SIZE = 10;

        public static readonly Page EMPTY_PAGE = new Page();

        private int pageSize = DEFAULT_PAGE_SIZE; // 每页的记录数

        private long start { get; set; } // 当前页第一条数据在List中的位置,从0开始

        private IList data; // 当前页中存放的记录,类型一般为List

        private long totalCount; // 总记录数

        public Page()
        {
            bind(0, 0, DEFAULT_PAGE_SIZE, new List<object>());
        }
        public Page(long totalSize)
        {
            bind(0, totalSize, DEFAULT_PAGE_SIZE, new List<object>());
        }
        public Page(long start, long totalSize, int pageSize, IList data)
        {
            bind(start, totalSize, pageSize, data);
        }
        private void bind(long start, long totalSize, int pageSize, IList data)
        {
            this.pageSize = pageSize;
            this.start = start;
            this.totalCount = totalSize;
            this.data = data;
        }
        public long GetTotalCount()
        {
            return this.totalCount;
        }
        /// <summary>
        /// 取总页数.
        /// </summary>
        /// <returns></returns>
        public long GetTotalPageCount()
        {
            if (totalCount % pageSize == 0)
                return totalCount / pageSize;
            else
                return totalCount / pageSize + 1;
        }

        /// <summary>
        ///  取每页数据容量.
        /// </summary>
        /// <returns></returns>
        public int GetPageSize()
        {
            return pageSize;
        }

        /// <summary>
        /// 取当前页中的记录.
        /// </summary>
        /// <returns></returns>
        public IList GetResult()
        {
            return data;
        }

        public void SetResult(IList data)
        {
            this.data = data;
        }

        /// <summary>
        /// 取该页当前页码,页码从1开始.
        /// </summary>
        /// <returns></returns>
        public long GetCurrentPageNo()
        {
            return start / pageSize + 1;
        }

        /// <summary>
        /// 该页是否有下一页.
        /// </summary>
        /// <returns></returns>
        public bool HasNextPage()
        {
            return this.GetCurrentPageNo() < this.GetTotalPageCount() - 1;
        }

        /// <summary>
        /// 该页是否有上一页.
        /// </summary>
        /// <returns></returns>
        public bool HasPreviousPage()
        {
            return this.GetCurrentPageNo() > 1;
        }

        /// <summary>
        /// 获取任一页第一条数据在数据集的位置，每页条数使用默认值.
        /// </summary>
        /// <param name="pageNo"></param>
        /// <returns></returns>
        protected static int GetStartOfPage(int pageNo)
        {
            return GetStartOfPage(pageNo, DEFAULT_PAGE_SIZE);
        }
        /// <summary>
        /// 获取任一页第一条数据在数据集的位置. 
        /// </summary>
        /// <param name="pageNo">从1开始的页号</param>
        /// <param name="pageSize">每页记录条数</param>
        /// <returns>该页第一条数据</returns>
        public static int GetStartOfPage(int pageNo, int pageSize)
        {
            return (pageNo - 1) * pageSize;
        }

        public static int GetPageNo(int rowIndex, int pageSize)
        {
            if (pageSize == 0) return -1;
            return rowIndex / pageSize + 1;
        }
    }
}
